package LoginServer;
import Library.ErrorCodes;
import Library.Login.ITicket;
import java.io.UnsupportedEncodingException;
import java.rmi.RemoteException;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.Map.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;

/**
 * 
 */
public class LoginServerManager extends javax.swing.JFrame
{
    /**
     * variabele van het type LoginServer
     */
    protected LoginServer m_LoginServer;

    /*
     * Creates new form LoginServerManager
    */
    /**
     *
     */
    public LoginServerManager()
    {
        initComponents();
        txtOutput.append("Starting LoginServer..\r\n");
        try
        {
            m_LoginServer = new LoginServer();
            if (m_LoginServer.startServer())
                txtOutput.append("LoginServer started on port " + Main.getSettings().getPort() + ".\r\n");
            else
                txtOutput.append("Failed to start LoginServer on port " + Main.getSettings().getPort() + ".\r\n");
        }
        catch(Exception ex)
        {
            txtOutput.append(ex.getMessage() + "\r\n");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        onbekendVoorwerp = new javax.swing.JList();
        jScrollPane4 = new javax.swing.JScrollPane();
        txtOutput = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        btnDeleteServer = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstGameServers = new javax.swing.JList();
        GameServerList = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        txtServerName = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        btnAddServer = new javax.swing.JButton();
        txtPort = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtIPadres = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        AccountList = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstAccounts = new javax.swing.JList();
        btnDeleteAccount = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        txtUsername = new javax.swing.JTextField();
        txtPassword = new javax.swing.JTextField();
        btnCreateAccount = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        lstTicket = new javax.swing.JList();
        TicketList = new javax.swing.JButton();

        jLabel1.setText("Server List");

        jLabel2.setText("Tickets List");

        jLabel3.setText("Accounts List");

        jScrollPane3.setViewportView(onbekendVoorwerp);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("LoginServer Manager");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        txtOutput.setColumns(20);
        txtOutput.setRows(5);
        jScrollPane4.setViewportView(txtOutput);

        jLabel4.setText("Logging");

        btnDeleteServer.setText("Delete Server");
        btnDeleteServer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteServerActionPerformed(evt);
            }
        });

        lstGameServers.setModel(new DefaultListModel());
        lstGameServers.setDoubleBuffered(true);
        jScrollPane1.setViewportView(lstGameServers);

        GameServerList.setText("Refresh GameServers");
        GameServerList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GameServerListActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("New Server"));

        txtServerName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtServerNameActionPerformed(evt);
            }
        });

        jLabel9.setText("Port");

        jLabel8.setText("IP address");

        btnAddServer.setText("Add Server");
        btnAddServer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddServerActionPerformed(evt);
            }
        });

        jLabel7.setText("Servername");

        txtIPadres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIPadresActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9))
                        .addGap(29, 29, 29)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtServerName)
                            .addComponent(txtPort)
                            .addComponent(txtIPadres, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(btnAddServer, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtServerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(7, 7, 7)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtIPadres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAddServer)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(GameServerList, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnDeleteServer)
                        .addContainerGap(279, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnDeleteServer)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 259, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(GameServerList)
                .addGap(167, 167, 167))
        );

        jTabbedPane1.addTab("Servers", jPanel1);

        AccountList.setText("Refresh Accounts");
        AccountList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AccountListActionPerformed(evt);
            }
        });

        lstAccounts.setModel(new DefaultListModel());
        jScrollPane2.setViewportView(lstAccounts);

        btnDeleteAccount.setText("Delete Account");
        btnDeleteAccount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteAccountActionPerformed(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("New Account"));

        btnCreateAccount.setText("Create Account");
        btnCreateAccount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateAccountActionPerformed(evt);
            }
        });

        jLabel5.setText("Username");

        jLabel6.setText("Password");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtPassword)
                            .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(btnCreateAccount)))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(6, 6, 6)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addComponent(btnCreateAccount)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(AccountList)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 121, Short.MAX_VALUE)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDeleteAccount)
                        .addContainerGap())))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnDeleteAccount)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(AccountList)
                .addGap(142, 142, 142))
        );

        jTabbedPane1.addTab("Accounts", jPanel3);

        lstTicket.setModel(new DefaultListModel());
        jScrollPane5.setViewportView(lstTicket);

        TicketList.setText("Refresh Tickets");
        TicketList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TicketListActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TicketList))
                .addGap(305, 305, 305))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(TicketList)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Tickets", jPanel5);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING)))
                .addContainerGap(10, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 334, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents



    private void btnCreateAccountActionPerformed(java.awt.event.ActionEvent evt)
    {
//GEN-FIRST:event_btnCreateAccountActionPerformed
        if (txtUsername.getText().isEmpty() || txtPassword.getText().isEmpty())
            txtOutput.setText("Please Specify a username and password.\r\n");
        else
        {
            String username = txtUsername.getText();
            int password = txtPassword.getText().hashCode();
            int result = ErrorCodes.ERROR_UNKNOWN;
            try
            {
                result = m_LoginServer.createAccount(username, password);
            }
            catch (RemoteException ex)
            {
                ex.printStackTrace();
            }
            catch (UnsupportedEncodingException ex)
            {
                ex.printStackTrace();
            }
            catch (NoSuchAlgorithmException ex)
            {
                ex.printStackTrace();
            }
            txtOutput.setText("Create Account '" + username + "': " + ErrorCodes.getMessage(result));
            refreshAccountList();
        }
    }//GEN-LAST:event_btnCreateAccountActionPerformed

    private void btnDeleteAccountActionPerformed(java.awt.event.ActionEvent evt)
    {
//GEN-FIRST:event_btnDeleteAccountActionPerformed
        Object username = lstAccounts.getSelectedValue();
        if (username != null)
        {
            int result = ErrorCodes.ERROR_UNKNOWN;
            try {
                result = m_LoginServer.deleteAccount(username.toString());
            } catch (RemoteException ex) {
                ex.printStackTrace();
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            } catch (NoSuchAlgorithmException ex) {
                ex.printStackTrace();
            }
            txtOutput.setText("Remove Account '" + username + "': " + ErrorCodes.getMessage(result));
            refreshAccountList();
        }
    }//GEN-LAST:event_btnDeleteAccountActionPerformed

    private void btnAddServerActionPerformed(java.awt.event.ActionEvent evt)
    {
//GEN-FIRST:event_btnAddServerActionPerformed
        if (txtServerName.getText().isEmpty() || txtIPadres.getText().isEmpty()
                || txtIPadres.getText().isEmpty())
            txtOutput.setText("Please specify a ServerName, IPAddress and Port.\r\n");
        else
        {
            String servername = txtServerName.getText();
            String IP_Port = txtIPadres.getText()+ ":" + txtPort.getText();

            int result = ErrorCodes.ERROR_UNKNOWN;
            try
            {
                result = m_LoginServer.addServer(servername, IP_Port);
            }
            catch (RemoteException ex)
            {
                ex.printStackTrace();
            }
            catch (UnsupportedEncodingException ex)
            {
                ex.printStackTrace();
            }
            catch (NoSuchAlgorithmException ex)
            {
                ex.printStackTrace();
            }
            txtOutput.setText("Adding Server '" + servername + "': " + ErrorCodes.getMessage(result));
            refreshGameServerList();
        }
    }//GEN-LAST:event_btnAddServerActionPerformed

    private void btnDeleteServerActionPerformed(java.awt.event.ActionEvent evt)
    {
//GEN-FIRST:event_btnDeleteServerActionPerformed
        String serverName = null;
        Object server = lstGameServers.getSelectedValue();
        if (server != null)
        {
            int result = ErrorCodes.ERROR_UNKNOWN;
            try {
                int index = server.toString().indexOf(" ");
                serverName = server.toString().substring(0, index);
                result =  m_LoginServer.deleteServer(serverName);
            } catch (RemoteException ex) {
                ex.printStackTrace();
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            } catch (NoSuchAlgorithmException ex) {
                ex.printStackTrace();
            }

            txtOutput.setText("Delete Server '" + serverName + "': " + ErrorCodes.getMessage(result));
            refreshGameServerList();
        }
    }//GEN-LAST:event_btnDeleteServerActionPerformed

    /**
     * Vult JTextfield txtGameServers met Gameserver namen en IP adressen.
     */
    private void GameServerListActionPerformed(java.awt.event.ActionEvent evt)
    {
//GEN-FIRST:event_GameServerListActionPerformed
        refreshGameServerList();
    }//GEN-LAST:event_GameServerListActionPerformed

    /**
     * Leegt JList txtTickets en vult deze opnieuw aan met m_Tickets uit de loginserver
     */
    private void TicketListActionPerformed(java.awt.event.ActionEvent evt)
    {
//GEN-FIRST:event_TicketListActionPerformed
        refreshTicketList();
    }//GEN-LAST:event_TicketListActionPerformed

    /**
     * Button Click leegt JList lstAccounts en vult deze opnieuw aan met
     * m_Accounts uit de loginserver
     */
    private void AccountListActionPerformed(java.awt.event.ActionEvent evt)
    {
//GEN-FIRST:event_AccountListActionPerformed
        refreshAccountList();
    }//GEN-LAST:event_AccountListActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
         try
            {
               if(m_LoginServer != null)
               {
                    m_LoginServer.dispose();
                    m_LoginServer = null;
                }
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
    }//GEN-LAST:event_formWindowClosing

    private void txtServerNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtServerNameActionPerformed
        // OBSOLETE
    }//GEN-LAST:event_txtServerNameActionPerformed

    private void txtIPadresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIPadresActionPerformed
        // OBSOLETE
    }//GEN-LAST:event_txtIPadresActionPerformed


    /**
     * Leegt JList GameServerList, en vult deze opnieuw vanuit map uit LoginServer
     */
    public void refreshGameServerList()
    {
        DefaultListModel mdl = (DefaultListModel)lstGameServers.getModel();
        mdl.clear();
        Set set  = m_LoginServer.getGameServers().keySet();
        Map map = m_LoginServer.getGameServers();
        Iterator iter = set.iterator();
        while (iter.hasNext())
        {
            String key = iter.next().toString();

            if (map.containsKey(key))
            {
                mdl.addElement(key + "  " +  map.get(key).toString());
            }
        }
    }

    /**
     * Leegt JList AccountList, en vult deze opnieuw vanuit map uit LoginServer
     */
    public void refreshAccountList()
    {
        DefaultListModel mdl = (DefaultListModel)lstAccounts.getModel();
        mdl.clear();
        Set set  = m_LoginServer.getAccounts().keySet();
        Map map = m_LoginServer.getAccounts();
        Iterator iter = set.iterator();
        while (iter.hasNext())
        {
            String key = iter.next().toString();
            if (map.containsKey(key))
            {
                mdl.addElement(key);
            }
        }
    }

    /**
     * Leegt JList TicketList, en vult deze opnieuw vanuit List uit LoginServer
     */
    public void refreshTicketList()
    {
        DefaultListModel mdl = (DefaultListModel)lstTicket.getModel();
        mdl.clear();
        List <ITicket> tickets = null;
        tickets = m_LoginServer.getTickets();
        ITicket ticket = null;
        if (tickets.isEmpty())
        {
            txtOutput.setText("Er zijn (nog) geen tickets aangemaakt.");
        }
        else
        {
            Iterator iter = tickets.iterator();
            while(iter.hasNext())
            {
                try {
                    ticket = (Ticket) iter.next();
                    mdl.addElement(ticket.getUsername() + "  " + ticket.getToken().toString());
                } catch (RemoteException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AccountList;
    private javax.swing.JButton GameServerList;
    private javax.swing.JButton TicketList;
    private javax.swing.JButton btnAddServer;
    private javax.swing.JButton btnCreateAccount;
    private javax.swing.JButton btnDeleteAccount;
    private javax.swing.JButton btnDeleteServer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JList lstAccounts;
    private javax.swing.JList lstGameServers;
    private javax.swing.JList lstTicket;
    private javax.swing.JList onbekendVoorwerp;
    private javax.swing.JTextField txtIPadres;
    private javax.swing.JTextArea txtOutput;
    private javax.swing.JTextField txtPassword;
    private javax.swing.JTextField txtPort;
    private javax.swing.JTextField txtServerName;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables

}
